package com.hamster.crm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.hamster.crm.mapper.CourseTaskMapper;
import com.hamster.crm.mapper.CourseTaskResultMapper;
import com.hamster.crm.pojo.CourseTask;
import com.hamster.crm.pojo.CourseTaskResult;
import com.hamster.crm.service.CourseTaskService;
import com.hamster.crm.util.PageResult;
import io.micrometer.core.instrument.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 课程任务的service
 * @author Tarry
 * @create 2019/12/19 14:21
 */
@Service
public class CourseTaskServiceImpl implements CourseTaskService {
    @Autowired
    private CourseTaskMapper courseTaskMapper;
    @Autowired
    private CourseTaskResultMapper courseTaskResultMapper;

    /**
     * 新增课程任务
     * @param courseTask
     */
    @Override
    public void insertCourseTask(CourseTask courseTask) {
        Date date = new Date();
        courseTask.setCreateTime(date);  //创建日期
        courseTask.setState("0");     //状态，0新建   1已下发   2完成
        courseTask.setInputUser("功能待完善");  //当前用户名
        courseTask.setInputTime(date);   //录入日期
        courseTask.setDel("0");       //删除标记 0 正常
        courseTaskMapper.insert(courseTask);
    }

    /**
     * 后台查询全部课程任务列表
     * @return List<CourseTask>
     */
    @Override
    public List<CourseTask> listCourseTasks() {
//        QueryWrapper<CourseTask> wrapper = new QueryWrapper<>();
//        wrapper.eq("del","0");
//        return   courseTaskMapper.selectList(wrapper);

       return courseTaskMapper.queryList();
    }

    /**
     * 删除课程任务，根据id
     * @param ids
     */
    @Transactional
    @Override
    public void deleteCourseTaskById(List<String> ids) {
        for (String id : ids) {
            courseTaskMapper.deleteTag(id) ;
        }
    }

    /**
     * 下发课程任务，改变状态值
     * @param ids
     */
    @Override
    public void updateCourseTaskStateById(List<String> ids) {
        for (String id : ids) {
            courseTaskMapper.updateCourseTaskStateById(id,"1");
        }
    }

    /**
     * 条件查询 参课结果列表
     * @param title 任务名称
     * @param participant 参与员工
     * @param currentPage 当前页
     * @param pageSize    每页条数
     * @return
     */
    @Override
    public PageResult<CourseTaskResult> listCourseTaskResults(String title, String participant, Integer currentPage, Integer pageSize) {
        //分页
        Page<CourseTaskResult> page = new Page<>(currentPage, pageSize);
        QueryWrapper<CourseTaskResult> wrapper = new QueryWrapper<>();

        wrapper.eq("del","0").orderByDesc("id");


        wrapper.groupBy("course_task_id","participant");//该条为2020年1月13日15:31:03添加的，为了分组查询


        //根据任务名称过滤
        if (StringUtils.isNotBlank(title)){
            wrapper.like("course_task_title","%"+title+"%");
        }
        //根据参与人员名过滤
        if (StringUtils.isNotBlank(participant)){
            wrapper.like("participant_name","%"+participant+"%");
        }

        IPage pageList = courseTaskResultMapper.selectPage(page, wrapper);
        List<CourseTaskResult> currentList =pageList.getRecords();
        //这里将设置 完成率 为综合完成率
        for (CourseTaskResult courseTaskResult : currentList) {
            courseTaskResult.setSelectivity(this.queryEmployeeTaskRate(courseTaskResult.getParticipant(),courseTaskResult.getCourseTaskId()));
        }
        Long pages = pageList.getPages();
        return new PageResult<CourseTaskResult>(pageList.getTotal(),pages.intValue(),currentList);
    }

    /**
     * 查询该员工  该任务的 客户执行 详细
     * @param employeeId
     * @param taskId
     * @return
     */
    @Override
    public List<CourseTaskResult> listTaskByEmployeeId(String employeeId, String taskId) {
        QueryWrapper<CourseTaskResult> courseTaskResultQueryWrapper = new QueryWrapper<>();
        courseTaskResultQueryWrapper.eq("course_task_id",taskId).eq("participant",employeeId);
        return courseTaskResultMapper.selectList(courseTaskResultQueryWrapper);
    }

    /**
     * 查询员工该任务手下所有人的综合参课率
     */
    private Double queryEmployeeTaskRate(String employeeId, String taskId){
        QueryWrapper<CourseTaskResult> courseTaskResultQueryWrapper = new QueryWrapper<>();
        courseTaskResultQueryWrapper.eq("course_task_id",taskId).eq("participant",employeeId);
        List<CourseTaskResult> resultList = courseTaskResultMapper.selectList(courseTaskResultQueryWrapper);
        Double y = Double.valueOf(resultList.size());//分母
        Double x = 0.0; //分子
        for (CourseTaskResult courseTaskResult : resultList) {
            x += courseTaskResult.getSelectivity();
        }
        return x/y;
    }

}
